package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.IHomeworkDomain;
import orm.CollectivityWeekTimeBucket;
import util.DBHelper;

public class IHomeworkDomainDAO {
	
	public void insertIHomeworkDomain(Statement statement, 
			ArrayList<IHomeworkDomain> ihwdArr, String tpID) throws SQLException {
		IHomeworkDomain ihwd = null;
		for (int i = 0; i < ihwdArr.size(); i++) {
			ihwd = ihwdArr.get(i);
			statement.execute("insert into ihomeworkdomain values('" + tpID + "'," +
					ihwd.getDomainID() + ", '" + ihwd.getRemark() + "')");
		}
	}
	
	public void updateIHomeworkDomain(Statement statement,
			ArrayList<IHomeworkDomain> ihwdArr, String tpID) throws SQLException {
		IHomeworkDomain ihwd = null;
		for (int i = 0; i < ihwdArr.size(); i++) {
			ihwd = ihwdArr.get(i);

			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update ihomeworkdomain set Remark = '" + ihwd.getRemark());
			sqlStr.append("' where TPID = '" + tpID + "' and DomainID = " + ihwd.getDomainID() + "");
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIHomeworkDomain(Statement statement, String tpID) throws SQLException {
		statement.execute("delete from ihomeworkdomain where TPID='" + tpID + "'");
	}
	
	public ArrayList<IHomeworkDomain> getIHWDByTPID(String tpID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IHomeworkDomain> al = new ArrayList<IHomeworkDomain>();
		IHomeworkDomain ihwd = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from ihomeworkdomain where TPID='" + tpID + "'");
		
		while (rs.next()) {
			ihwd = new IHomeworkDomain();
			int i = 1;
			ihwd.setTPID(rs.getString(i++));
			ihwd.setDomainID(rs.getInt(i++));
			ihwd.setRemark(rs.getString(i++));
			al.add(ihwd);
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
